package myJs.tool

import myJs.myPkg.{FileInputOptions, LayerOptions}
import myJs.myPkg.jquery._

import scala.scalajs.js.annotation.JSExportTopLevel
import myJs.Implicits._

import scala.scalajs.js
import scalatags.Text.all._

/**
 * Created by yz on 17/9/2021
 */
@JSExportTopLevel("Tool")
object Tool extends ProjectTool with TableFilterTool {

  val pattern = "yyyy-mm-dd"
  val confirmButtonClass = "btn-danger"
  val cancelButtonClass = "green"
  val zhRunning = "正在运行"
  val myElement = div(id := "content")(
    span(id := "info")(zhRunning),
    " ",
    img(src := "/assets/images/running2.gif", width := 30, height := 20, cls := "runningImage")
  ).render
  val zhInfo = "信息"
  val layerOptions = LayerOptions.title(zhInfo).closeBtn(0).skin("layui-layer-molv").btn(js.Array())

  def getInputByName(name: String) = {
    $(s":input[name='${name}']")
  }

  def fileInput = {
    val options = FileInputOptions.showPreview(false).browseLabel(s"选择...").
      removeLabel("删除文件").language("zh").showUpload(false)
    $(".file").fileinput("destroy")
    $(".file").fileinput(options)
  }

  def getParentElem(parentId: String) = {
    if (parentId.isEmpty) "body" else s"#${parentId}"
  }

  def getParentJq(parentId: String) = {
    val y = getParentElem(parentId)
    $(y)
  }

  def getIdsValue(parentId: String = ""): List[String] = {
    val parentJq = Tool.getParentJq(parentId)
    getIdsValue(parentJq)
  }

  def getIdsValue(parentJq: JQuery): List[String] = {
    val arrays = parentJq.find(s"#table").bootstrapTable("getSelections").toJArJDJAn
    arrays.map { x =>
      x("id").toString
    }.toList
  }

  def getIds(parentId: String = ""): JQuery = {
    val parentJq = Tool.getParentJq(parentId)
    getIds(parentJq)
  }

  def getIds(parentJq: JQuery): JQuery = {
    val ids = getIdsValue(parentJq)
    if (ids.isEmpty) {
      parentJq.find(s".idsButton").attr("disabled", true)
    } else {
      parentJq.find(s".idsButton").attr("disabled", false)
    }
  }

  def bindEvt(parentId: String = ""): JQuery = {
    val parentJq = Tool.getParentJq(parentId)
    bindEvt(parentJq)
  }

  def bindEvt(parentJq: JQuery): JQuery = {
    val tableId = "table"
    val tableJq = parentJq.find(s"#${tableId}")
    tableJq.on("check.bs.table", () => getIds(parentJq)).on("uncheck.bs.table", () => getIds(parentJq)).
      on("check-all.bs.table", () => getIds(parentJq)).on("uncheck-all.bs.table", () => getIds(parentJq)).
      on("page-change.bs.table", () => getIds(parentJq))
  }

  def fillByNames(rs: js.Dictionary[String]):Unit = {
    val parentJq = Tool.getParentJq("")
    fillByNames(rs, parentJq)
  }

  def fillByNames(rs: js.Dictionary[String], parentJq: JQuery):Unit = {
    val names = parentJq.find(s".fillByName").mapElems { y =>
      $(y).attr("name").toString
    }.toArray
    names.foreach { name =>
      fillByName(rs, name, parentJq)
    }
  }

  def fillByName(rs: js.Dictionary[String], name: String, parentJq: JQuery) = {
    val valOp = rs.get(name)
    valOp.foreach { value =>
      val jq = parentJq.findInputByName(name)
      jq.`val`(value)
      if (jq.hasClass("myMinicolors")) {
        jq.minicolors("value", js.Dictionary("color" -> s"${value}"))
      }
    }
  }

  def popover = {
    val options = js.Dictionary("container" -> "body", "trigger" -> "hover")
    $(".selfQuestion").popover(options)
  }

}
